package com.wobito.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wobito.common.constants.BusinessException;
import com.wobito.common.constants.ErrorCode;
import com.wobito.common.utils.DateUtils;
import com.wobito.mapper.ConfigDataMapper;
import com.wobito.pojo.bean.ConfigData;
import com.wobito.pojo.dto.config.ConfigDataDTO;
import com.wobito.pojo.dto.config.ConfigPictureDTO;
import com.wobito.pojo.vo.config.AddConfigDataVO;
import com.wobito.pojo.vo.config.ConfigDataVO;
import com.wobito.pojo.vo.config.UpdateConfigDataVO;
import com.wobito.service.IConfigDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lambyang
 * @since 2024-08-16
 */
@Service
public class ConfigDataServiceImpl extends ServiceImpl<ConfigDataMapper, ConfigData> implements IConfigDataService {

    @Autowired
    private ConfigDataMapper configDataMapper;


    @Override
    public List<ConfigDataDTO> selectConfigList(ConfigDataVO configDataVO) {
        List<ConfigDataDTO> configDataDTOS = new ArrayList<>();
        List<ConfigData> configDatas = configDataMapper.selectList(new LambdaQueryWrapper<ConfigData>()
                .eq(ConfigData::getConfigKey, configDataVO.getConfigKey()));
        if (!CollectionUtils.isEmpty(configDatas)) {
            configDataDTOS = configDatas.stream().map(config -> {
                ConfigDataDTO configDataDTO = BeanUtil.copyProperties(config, ConfigDataDTO.class);
                return configDataDTO;
            }).collect(Collectors.toList());
        }
        return configDataDTOS;
    }

    @Override
    public int insertConfigData(AddConfigDataVO addConfigDataVO) {
        ConfigData configData = BeanUtil.copyProperties(addConfigDataVO, ConfigData.class);
        return configDataMapper.insert(configData);
    }

    @Override
    public int addPicture(String configKey, ConfigPictureDTO pictureDTO) {
        List<ConfigData> configDatas=configDataMapper.selectList(new LambdaQueryWrapper<ConfigData>().eq(ConfigData::getConfigKey, configKey));
        if(CollectionUtil.isNotEmpty(configDatas)){
            if (configDatas.stream().anyMatch(d->d.getConfigLabel().equals(pictureDTO.getIndex()))){
                throw new BusinessException(ErrorCode.FORBIDDEN,"该配置项下已存在相同的图片索引");
            }
        }
        return configDataMapper.insert(new ConfigData(pictureDTO.getIndex(), pictureDTO.getUrl(),configKey));
    }

    @Override
    public int updateConfigData(UpdateConfigDataVO updateConfigDataVO) {
        ConfigData configData = BeanUtil.copyProperties(updateConfigDataVO, ConfigData.class);
        configData.setId(updateConfigDataVO.getConfigId());
        configData.setUpdateTime(DateUtils.getNowDate());
        return configDataMapper.updateById(configData);
    }
}
